import json
import re
import sys
import os
from configparser import ConfigParser
from openpyxl import load_workbook
from Write2Excel import Write2Excel

def recursive_traversal(dir, do_what):
    if not os.path.isdir (dir):
        do_what(dir)
        return
    files = os.listdir (dir)
    for file in files:
        file_abs_path = os.path.join(dir, file)
        if os.path.isdir (file_abs_path):
             recursive_traversal(file_abs_path,do_what)
        else:
            do_what(file_abs_path)



mResult = {}
def collect_string_xml(file_abs_path):
    if file_abs_path.endswith("strings.xml"):
        f=open(file_abs_path).read()
        lang =None
        if file_abs_path.endswith("values/strings.xml"):
            lang ="en"
        else :
            lang =file_abs_path[-(len("strings.xml")+3):-(len("strings.xml")+1)]
        if lang =="iw":
            lang="he"
        elif lang=="in":
            lang= "id"

        for key in key_arr:
            search_result = re.search(r""+'<string\s+name\s*=\s*"'+key+'"\s*>([\s\S]+?)</\s*string\s*>',f, re.I)
            if search_result:
                if not key in mResult:
                    mResult[key]={}
                lang_map=mResult[key]
                lang_map[lang]= search_result.group(1)








def write2Excel(write2ExcelObj,text, cell_name) :
    write2ExcelObj.write(text, cell_name)

# shipping_info_option 需要考查一下，德国
# address line 1 如果后台没返回的话则使用 app 端写死的
# address line 2 同上
# land_mark 是什么东西
# zip code 直接用的就是 app 端的，根据后台 need_zipcode 字段
# state 都是取接口但是会做一些拼接 * 跟 Optional，下拉是拼接 *，非下拉是拼接 (optional)
# state 新增地址的时候其标题为 app 端写死的
# city spinner 跟 et 用的是接口返回的，title 用的是 app 端写死的
# region 仅马来西亚有，app 端写死的
# pcc code 是什么东西
# email app 端写死的
# phone app 端写死的

key_arr =["shipping_first_name","shipping_last_name","address_line_1","region","shipping_zip_optional","shipping_zip","shipping_states","shipping_city","shipping_email","shipping_phone","optional"]
cell_name_arr=["E3","E4","E5" ,"E6","E7","E7","E8","E9","E10","E11","E12"]

if __name__ == '__main__':


    write2ExcelObj=  Write2Excel("/Users/h/Downloads/地址配置说明文档.xlsx", "Sheet2")
    recursive_traversal("/Users/h/Projects/app-rn/android/app/src/main/res",collect_string_xml )

    start_column =ord('C')


    for i,key in enumerate(key_arr):
        if key in mResult:
            lang_map = mResult[key]
            for index, lang in enumerate(lang_map):
                if i ==0:
                    cell_name=chr(start_column + index)+str(i+1)
                    print(lang)
                    write2Excel(write2ExcelObj,lang, cell_name)
                cell_name=chr(start_column + index)+str(i+2)

                write2Excel(write2ExcelObj,lang_map[lang], cell_name)


